﻿<%@ WebHandler Language="C#" Class="_1007_2" %>

using System;
using System.Web;
using CCH.Entity;
using CCH.DAO;
using System.IO;
using System.Linq;

public class _1007_2 : IHttpHandler {
    
    public void ProcessRequest (HttpContext context) {
        
        byte[] img_bin = null;
        
        
        

        using (Entities model = new Entities())
        {
            if (context.Request["alb_no"] != null)
            {
                int alb_no = int.Parse(context.Request["alb_no"]);
                
                //查詢有無封面
                var photo = (
                             from p in model.album
                             where p.alb_no == alb_no
                             from d in model.photo
                             where d.pho_no == p.alb_cover && d.alb_no == alb_no
                             select d).FirstOrDefault();

                if (photo != null)
                {
                    string filepath = photo.pho_file;

                    if (File.Exists(filepath))
                    {
                        img_bin = File.ReadAllBytes(filepath);
                    }
                }
                else
                {
                    //沒封面取第一張
                    var photo2 = (from d in model.photo where d.alb_no == alb_no orderby d.pho_no select d).FirstOrDefault();

                    if (photo2 != null)
                    {
                        string filepath = photo2.pho_file;

                        if (File.Exists(filepath))
                        {
                            img_bin = File.ReadAllBytes(filepath);
                        }
                    }
                }
            }
            
            //照片列表
            if (context.Request["alb_no"] != null && context.Request["pho_no"] != null && context.Request["type"] != null)
            {
                int alb_no = int.Parse(context.Request["alb_no"]);
                int pho_no = int.Parse(context.Request["pho_no"]);
                var data = (from d in model.photo where d.pho_no == pho_no && d.alb_no == alb_no select d).FirstOrDefault();
                if (data != null)
                {
                    string filepath = "";

                    if (context.Request["type"] == "1")
                    {
                        filepath = data.pho_thumb;
                    }
                    else
                    {
                        filepath = data.pho_file;
                    }

                    if (File.Exists(filepath))
                    {
                        img_bin = File.ReadAllBytes(filepath);
                    }
                }
            }
            

        }

        if (img_bin == null)
        {
            img_bin = File.ReadAllBytes(context.Server.MapPath("~/images/noimage.jpg"));
        }

        //輸出
        context.Response.ContentType = "image/jpeg";
        context.Response.BinaryWrite(img_bin);
        context.Response.Flush();
        context.Response.End();
    }
 
    public bool IsReusable {
        get {
            return false;
        }
    }

}